/*
 * @Author: xiekai 
 * @Date: 2020-04-28 13:57:34 
 * @Last Modified by: lusx
 * @Last Modified time: 2020-05-14 13:55:29
 */
import React, { Component } from 'react';
import { SearchBar, Tools } from 'weaver-mobile';
import { SearchPage } from 'weaver-mobile-page';
import { observer, inject } from 'mobx-react';
import { toJS } from 'mobx';
import Item from './list/item';
const getLabel = Tools.getLabel;
const template = {
    default: [
        {
            key: 'col1',
            configs: [
                {
                    key: 'col1_row1',
                    configs: [
                        { key: 'name' },
                    ],
                },
            ],
        },
    ],
};
@inject("createProjectStore")
@observer
export default class New extends Component {

    componentWillMount() {
        const { searchPage, initDatas } = this.props.createProjectStore;
        initDatas();
    }

    componentDidMount() {
        const { searchPage } = this.props.createProjectStore;
        searchPage.setState({ template, placeholder: getLabel(1, "请输入督办类型名称/首字母/id") });
        let data = Tools.ss.getJSONObj("gvAgentListData");
        searchPage.list.setState({ data: data || [] });
    }

    render() {
        const { searchPage } = this.props.createProjectStore;
        return (
            <div className="gv-add-wrap">
                <div className="gv_add">
                    <div style={{ height: "100%" }}>
                        <SearchBar
                            placeholder={getLabel(391111, "请输入督办类型名称/首字母/id")}
                            value={searchPage.searchValue}
                            onFocus={() => {
                                const { history } = this.props;
                                history.push(searchPage.path);
                            }}
                        />
                        <SearchPage
                            store={searchPage}
                            type={1}
                            nameKey="name"
                            rowKey="key"
                            onClear={e => {
                                const { searchPage } = this.props.createProjectStore;
                                const { list } = searchPage;
                                list.setState({ data: [] });
                            }}
                            onSubmit={(e) => {
                                const { setState, datas, changeAccordionIcon, searchPage, activeKey } = this.props.createProjectStore;
                                let fu, fu2;
                                if (toJS(e).name) {
                                    fu = toJS(e).name.toUpperCase();
                                    fu2 = toJS(e).name;
                                } else {
                                    fu = e.toUpperCase();
                                    fu2 = e;
                                }
                                if (fu === "") {
                                    changeAccordionIcon(activeKey, "")
                                    setState({ isFuzzy: false, activeKey: [] })
                                } else {
                                    setState({ isFuzzy: true })
                                    //刷新模糊搜索的数据
                                    let types = [];
                                    toJS(datas).map(item => {
                                        let wfbeans = [];
                                        item.wfbeans.map(wfbean => {
                                            if (wfbean.name.toUpperCase().includes(fu) || wfbean.id.toUpperCase().includes(fu) || wfbean.spell.toUpperCase().includes(fu)) {
                                                wfbeans.push(wfbean);
                                            }
                                            return wfbeans;
                                        })
                                        if (wfbeans.length > 0) {
                                            item.wfbeans = wfbeans;
                                            types.push(item);
                                        }
                                        return types;
                                    })
                                    let activeKey1 = [];
                                    types.map(items => {
                                        return activeKey1.push(items.id);
                                    })
                                    setState({ searchValue: fu, searchValue_default: fu2 });
                                    changeAccordionIcon(activeKey1, "true")
                                    setState({ datasM: types });

                                    //保存搜索历史
                                    let arr = toJS(searchPage.list.data);
                                    if (arr.length <= 0) {
                                        arr.push({ name: fu2, key: "0" })
                                    } else {
                                        let index = -1;
                                        let flag = false;//输入的e是否已在记录中
                                        arr.map(item => {
                                            if (item.name === fu2) {
                                                flag = true;
                                                index = parseInt(item.key, 10);
                                            }
                                            return index;
                                        })
                                        if (flag) {//已在记录中
                                            arr.splice(index, 1);
                                            arr.slice(0, index).forEach(item => {
                                                item.key = (parseInt(item.key, 10) + 1).toString();
                                            })
                                            arr.unshift({ name: fu2, key: "0" })
                                        } else {//不再记录中
                                            arr.forEach(item => {
                                                item.key = (parseInt(item.key, 10) + 1).toString();
                                            })
                                            arr.unshift({ name: fu2, key: "0" });
                                            if (arr.length > 5) {
                                                arr.pop();
                                            }
                                        }
                                    }
                                    Tools.ss.set("gvAgentListData", arr)
                                    searchPage.list.setState({ data: arr })
                                }
                            }}
                        />
                        <div className="gv-add-noTabs-wrap">
                            <div className="gv_add_judgeHeight gv_add_content ">
                                <div className="gv_add_wrap">
                                    <div className="gv_add_judgePl">
                                        <Item {...this.props}/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
}

